package com.app.navigationbar.common;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.res.ResourcesCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.app.navigationbar.R;
import com.zndroid.navigation.NavigationTabBarVP2;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lzy on 2020/11/30.
 */
public class CommonActivity extends FragmentActivity {
    private NavigationTabBarVP2 navigationBar;
    private ViewPager2 viewPager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_common);

        viewPager = findViewById(R.id.vp);
        navigationBar = findViewById(R.id.nb);

        initVP();
        initBar();
    }

    private void initVP() {
        viewPager.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                Bundle bundle = new Bundle();
                bundle.putString("msg", "#page " + position);
                Fragment fragment = new FirstFragment();
                fragment.setArguments(bundle);
                return fragment;
            }

            @Override
            public int getItemCount() {
                return 3;
            }
        });
    }

    private void initBar() {
        List<NavigationTabBarVP2.Model> models = new ArrayList<>();

        NavigationTabBarVP2.Model model0 = new NavigationTabBarVP2.Model.Builder(ResourcesCompat.getDrawable(getResources(), R.drawable.ic_first, null), Color.BLUE)
                .title("first")
                .selectedIcon(ResourcesCompat.getDrawable(getResources(), R.drawable.ic_fifth, getTheme()))
                .build();
        NavigationTabBarVP2.Model model1 = new NavigationTabBarVP2.Model.Builder(ResourcesCompat.getDrawable(getResources(), R.drawable.ic_second, null), Color.BLUE)
                .title("second")
                .selectedIcon(ResourcesCompat.getDrawable(getResources(), R.drawable.ic_fifth, getTheme()))
                .build();
        NavigationTabBarVP2.Model model2 = new NavigationTabBarVP2.Model.Builder(ResourcesCompat.getDrawable(getResources(), R.drawable.ic_third, null), Color.BLUE)
                .title("third")
                .selectedIcon(ResourcesCompat.getDrawable(getResources(), R.drawable.ic_fifth, getTheme()))
                .build();

        models.add(model0);
        models.add(model1);
        models.add(model2);

        navigationBar.setViewPager(viewPager, 1);
        navigationBar.setModels(models);
        navigationBar.setTitleMode(NavigationTabBarVP2.TitleMode.ALL);
        navigationBar.setBgColor(Color.parseColor("#ffffff"));
        navigationBar.setIsTitled(true);
        navigationBar.setIsShowLine(true);
        navigationBar.setLineWidth(6);
        navigationBar.setLineColor(Color.parseColor("#ff00f0"));
        navigationBar.setAnimationDuration(500);
    }
}
